/*
 * MTA应用程序UI界面所需要的模块
 * 计划逐渐从weui.css转向bootstrap
 */

//生成bootstrap样式的分页控件
function generatePaginator(middlePage, totalPages, currentPage, link) {
    let paginationLinks = "";

    const prevLink =
        currentPage > 1
            ? `<a class="page-link" href="${link}&page=${currentPage - 1}">&laquo;</a>`
            : '<span class="page-link" style="color:#6c757d;">&laquo;</span>';
    const nextLink =
        currentPage < totalPages
            ? `<a class="page-link" href="${link}&page=${currentPage + 1}">&raquo;</a>`
            : '<span class="page-link" style="color:#6c757d;">&raquo;</span>';

    if (totalPages <= middlePage) {
        for (let i = 1; i <= totalPages; i++) {
            if (i === currentPage) {
                paginationLinks += `<a class="page-link active" href="${link}&page=${i}">${i}</a>`;
            } else {
                paginationLinks += `<a class="page-link" href="${link}&page=${i}">${i}</a>`;
            }
        }
    } else {
        const startPage = Math.max(1, currentPage - Math.floor(middlePage / 2));
        const endPage = Math.min(startPage + middlePage - 1, totalPages);

        //if (startPage > 1)  paginationLinks += `<a class="page-link" href="${link}&page=${startPage - 1}">&laquo;</a>`;

        for (let i = startPage; i <= endPage; i++) {
            if (i === currentPage) {
                paginationLinks += `<a class="page-link active" href="${link}&page=${i}">${i}</a>`;
            } else {
                paginationLinks += `<a class="page-link" href="${link}&page=${i}">${i}</a>`;
            }
        }

        //if (endPage < totalPages) //paginationLinks += `<a class="page-link" href="${link}&page=${endPage + 1}">&raquo;</a>`;
    }

    return `<li class="page-item">${prevLink}</li> ${paginationLinks} <li class="page-item">${nextLink}</li>`;
}

const mui = { generatePaginator };
module.exports = mui;
